export default {
  //获取地址栏参数
  getUrlParams : function (_url) {
    var url = "",
      s_params = "";
    var arr = [],
      params = {},
      key,
      val;
    url = _url || window.location.search;
    if (url.lastIndexOf("?") >= 0) {
      s_params = url.split("?")[1];
      arr = s_params.split("&");
      for (var i = 0; i < arr.length; i++) {
        key = arr[i].split("=")[0];
        val = arr[i].split("=")[1];
        params[key] = val;
      }
    }
    return params;
  },
  ObjectToFormatDate : function (objectDate, formatstr, type = true) {
    try {

      //console.log(objectDate);
      let date = new Date(objectDate);

      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      let day = date.getDate();
      let min = date.getMinutes();
      let sec = date.getSeconds();
      let hour = date.getHours();
      //console.log(date);
      month = month.length === 2
        ? month
        : "0" + month;
      day = day
        .toString()
        .length === 2
        ? day
        : "0" + day;
      hour = hour
        .toString()
        .length === 2
        ? hour
        : "0" + hour;
      min = min
        .toString()
        .length === 2
        ? min
        : "0" + min;
      sec = sec
        .toString()
        .length === 2
        ? sec
        : "0" + sec;
      if (type) {
        return year + formatstr + month + formatstr + day + " " + hour + ":" + min + ":" + sec;
      } else {
        return year + formatstr + month + formatstr + day;
      }
    } catch (ex) {
      return objectDate;
    }
  },
  //格式化标准时间
  formateDate : function (_date, _type, _min, _sec) {
    if (va != null && va != '' && va != undefined && JSON.stringify(va).indexOf(_type) <= 0) {
      let month = va.getMonth() + 1;
      let date = va.getDate();
      let hour = va.getHours();
      let min = va.getMinutes();
      let sec = va.getSeconds();
      month = month.length === 2
        ? month
        : "0" + month;
      date = date
        .toString()
        .length === 2
        ? date
        : "0" + date;
      hour = hour
        .toString()
        .length === 2
        ? hour
        : "0" + hour;
      min = min
        .toString()
        .length === 2
        ? min
        : "0" + min;
      sec = sec
        .toString()
        .length === 2
        ? sec
        : "0" + sec;
      if (_min) {
        return "" + va.getFullYear() + _type + month + _type + date + " " + hour + ":" + min;
      }
      if (_sec) {
        return "" + va.getFullYear() + _type + month + _type + date + " " + hour + ":" + min + ":" + sec;
      }
      return "" + va.getFullYear() + _type + month + _type + date;
    } else 
      return va;
    }
  ,
  //加法函数
  accAdd : function (arg1, arg2) {
    var r1,
      r2,
      m;
    try {
      r1 = arg1
        .toString()
        .split(".")[1]
        .length;
    } catch (e) {
      r1 = 0;
    }
    try {
      r2 = arg2
        .toString()
        .split(".")[1]
        .length;
    } catch (e) {
      r2 = 0;
    }
    m = Math.pow(10, Math.max(r1, r2));
    return (arg1 * m + arg2 * m) / m;
  },

  //减法函数
  subtr : function (arg1, arg2) {
    var r1,
      r2,
      m,
      n;
    try {
      r1 = arg1
        .toString()
        .split(".")[1]
        .length;
    } catch (e) {
      r1 = 0;
    }
    try {
      r2 = arg2
        .toString()
        .split(".")[1]
        .length;
    } catch (e) {
      r2 = 0;
    }
    m = Math.pow(10, Math.max(r1, r2));
    //last modify by deeka 动态控制精度长度
    n = (r1 >= r2)
      ? r1
      : r2;
    return ((arg1 * m - arg2 * m) / m).toFixed(n);
  },

  //乘法函数
  accMul : function (arg1, arg2) {
    var m = 0,
      s1 = arg1.toString(),
      s2 = arg2.toString();
    try {
      m += s1.split(".")[1].length;
    } catch (e) {}
    try {
      m += s2.split(".")[1].length;
    } catch (e) {}
    return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
  },

  //深度拷贝对象函数
  deepClone : function (_data) {
    var result;
    function setObj(_obj) {
      if (_obj && typeof(_obj) === 'object') {
        if (Array.isArray(_obj)) {
          result = [];
          for (let i = 0; i < _obj.length; i++) {
            result.push(setObj(_obj[i]));
          }
          return result;
        } else {
          result = {};
          for (let key in _obj) {
            result[key] = setObj(_obj[key]);
          }
          return result;
        }
      } else {
        return _obj;
      }
    }
    return setObj(_data);
  },

  //将页面输入框重置为空
  clearInput : function () {
    setTimeout(function () {
      document
        .querySelector("input[type=text]")
        .value = "";
    }, 0);
  },
  //判断是否为ie浏览器
  identifyBrowser : function () {
    var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
    if (userAgent.toLowerCase().indexOf("trident") > -1) {
      return "1";
    } else {
      return "0";
    }
  }
};
